<template>
	<view class="index-container">
		<view v-if="personType == 1">
			<view class="banner">
				<image src="../../static/icon/banner.png" mode=""></image>
			</view>
			<!-- <view class="repair-box">
				<view class="left-item">
					<view class="top">报事报修</view>
					<view class="bottom">足不出户 轻松搞定</view>
				</view>
				<view class="go-repair">立即报修</view>
				<view class="img">
					<image src="../../static/icon/qn.png"></image>
				</view>
			</view> -->
			<view class="nav-box">
				<view class="nav-box-item" @click="goMyCheckPoint">
					<view class="nav-icon">
						<image src="../../static/icon/check.png" mode=""></image>
					</view>
					<view class="nav-desc">
						<view class="desc-top">检查点信息</view>
						<view class="desc-bottom">随时查看 轻松搞定</view>
					</view>
				</view>
				<view class="nav-box-item" @click="goAccount">
					<view class="nav-icon">
						<image src="../../static/icon/account.png" mode=""></image>
					</view>
					<view class="nav-desc">
						<view class="desc-top">经营台账</view>
						<view class="desc-bottom">及时查看 台账情况</view>
					</view>
				</view>
			</view>
			<view class="nav-box">
				<view class="nav-box-item" @click="goCheckItem">
					<view class="nav-icon">
						<image src="../../static/icon/left.png" mode=""></image>
					</view>
					<view class="nav-desc">
						<view class="desc-top">检查事项</view>
						<view class="desc-bottom">随时查看 轻松搞定</view>
					</view>
				</view>
				<view class="nav-box-item" @click="goCheckRank">
					<view class="nav-icon">
						<image src="../../static/icon/right.png" mode=""></image>
					</view>
					<view class="nav-desc">
						<view class="desc-top">巡检排行</view>
						<view class="desc-bottom">及时查看 排名情况</view>
					</view>
				</view>
			</view>
			<view class="rectify-task">
				<text style="font-size: 36rpx;color: #333;">整改任务</text>
				<view class="more" @click="goMoreRectify">查看更多</view>
			</view>
			<rectify-item v-if="rectifyList.length" :rectify-list="rectifyList"></rectify-item>
			<view v-else>
				<van-empty description="暂无任务" />
			</view>
			<uni-load-more v-if="rectifyList && rectifyList.length >= 2" :status="status1" />
		</view>
		<view v-else>
			<view class="base-info">
				<view class="left-icon"></view>
				基础信息
			</view>
			<view class="base-box" v-if="personType == 3">
				<view class="base-item">
					<view class="base-img">
						<image src="../../static/index/56.png"></image>
					</view>
				</view>
				<view class="base-item">
					<view class="item-num">
						{{gridNum}}
					</view>
					<text class="base-text">网格数</text>
				</view>
				<view class="base-item">
					<view class="item-num">
						{{gridLeadNum}}
					</view>
					<text class="base-text">网格长</text>
				</view>
				<view class="base-item">
					<view class="item-num">
						{{gridPersonNum}}
					</view>
					<text class="base-text">网格员</text>
				</view>
			</view>
			<view class="base-box">
				<view class="base-item">
					<view class="base-img">
						<image src="../../static/index/45.png"></image>
					</view>
				</view>
				<view class="base-item" @click="goResident(0)">
					<view class="item-num">
						{{residentNum}}
					</view>
					<text class="base-text">居民数量</text>
				</view>
				<view class="base-item" @click="goResident(2)">
					<view class="item-num">
						{{permanentNum}}
					</view>
					<text class="base-text">常住人口</text>
				</view>
				<view class="base-item" @click="goResident(1)">
					<view class="item-num">
						{{floatResidentNum}}
					</view>
					<text class="base-text">流动人口</text>
				</view>
			</view>
			<view class="base-box">
				<view class="base-item">
					<view class="base-img">
						<image src="../../static/index/46.png"></image>
					</view>
				</view>
				<view class="base-item" @click="goCheckPoint(0)">
					<view class="item-num">
						{{checkPointNum}}
					</view>
					<text class="base-text">检查点数</text>
				</view>
				<view class="base-item" @click="goCheckPoint(7)">
					<view class="item-num">
						{{roomNum}}
					</view>
					<text class="base-text">房屋数量</text>
				</view>
				<view class="base-item" @click="goCheckPoint(8)">
					<view class="item-num">
						{{rentHouseNum}}
					</view>
					<text class="base-text">出租房屋</text>
				</view>
			</view>
			<view class="base-box">
				<view class="base-item" @click="goCheckPoint(1)">
					<view class="item-num">
						{{companyNum}}
					</view>
					<text class="base-text">企业数量</text>
				</view>
				<view class="base-item" @click="goCheckPoint(2)">
					<view class="item-num">
						{{villageNum}}
					</view>
					<text class="base-text">小区数量</text>
				</view>
				<view class="base-item" @click="goCheckPoint(5)">
					<view class="item-num">
						{{shopNum}}
					</view>
					<text class="base-text">商铺数量</text>
				</view>
				<view class="base-item" @click="goCheckPoint(3)">
					<view class="item-num">
						{{restaurantNum}}
					</view>
					<text class="base-text">餐饮店</text>
				</view>
				
			</view>
			<view class="base-box">
				<view class="base-item" @click="goCheckPoint(4)">
					<view class="item-num">
						{{clinicNum}}
					</view>
					<text class="base-text">个体诊所</text>
				</view>
				<view class="base-item" @click="goCheckPoint(6)">
					<view class="item-num">
						{{publicNum}}
					</view>
					<text class="base-text">其他</text>
				</view>
			</view>
			<view class="base-box">
				<view class="base-item">
					<view class="base-img">
						<image src="../../static/index/47.png"></image>
					</view>
				</view>
				<view class="base-item" @click="goInstallList">
					<view class="item-num">
						{{facilityNum}}
					</view>
					<text class="base-text">设施数量</text>
				</view>
			</view>
			<view class="base-info">
				<view class="left-icon"></view>
				功能入口
			</view>
			<view class="nav-index">
				<view class="nav-item" v-for="(item,index) in navList" :key="index" @click="navItemClick(item.path)">
					<view class="item-img">
						<image :src="item.src"></image>
					</view>
					<text class="item-text">{{item.title}}</text>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import CheckItem from '../components/checkItem.vue'
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import dayjs from 'dayjs'
	import RectifyItem from '../components/rectifyItem'
	export default {
		components: {
			CheckItem,
			RectifyItem
		},
		data() {
			return {
				status1: 'more',
				rectifyList: [],
				rentHouseNum: 0,
				residentNum: 0,
				floatResidentNum: 0,
				permanentNum: 0,
				gridNum: 0,
				gridLeadNum: 0,
				gridPersonNum: 0,
				checkPointNum: 0,
				roomNum: 0,
				shopNum: 0,
				restaurantNum: 0,
				clinicNum: 0,
				companyNum: 0,
				facilityNum: 0,
				villageNum: 0,
				publicNum: 0,
				params: {
					pagination: {
						current: 1,
						pageSize: 10
					},
					sorter: {
						field: "id",
						order: 0
					}
				},
				params1: {
					pagination: {
						current: 1,
						pageSize: 10
					},
					sorter: {
						field: "id",
						order: 0
					}
				},
				params2: {
					所属网格id: null,
					整改状态: 2,
					负责人电话: null,
					pagination: {
						current: 1,
						pageSize: 3
					},
					sorter: {
						field: "id",
						order: 0
					}
				},
				navList: [{
						src: '../../static/index/48.png',
						title: '信息采集',
						path: '/fireGridCheck/collectMessage/collectMessage'
					},
					{
						src: '../../static/index/49.png',
						title: '新建巡检',
						path: '/fireGridCheck/createCheck/createCheck'
					},
					{
						src: '../../static/index/50.png',
						title: '新建整改',
						path: '/fireGridCheck/createRectify/createRectify'
					},
					{
						src: '../../static/index/51.png',
						title: '整改任务',
						path: '/fireGridCheck/rectifyTask/rectifyTask'
					}, {
						src: '../../static/index/52.png',
						title: '巡检任务',
						path: '/fireGridCheck/checkTask/checkTask'
					}, {
						src: '../../static/index/54.png',
						title: '更多功能',
						path: '/pages/firecontrol/firecontrol'
					}
				]
			}
		},
		onLoad(options) {
			if (options.q) {
				console.log(options.q)
				let str = decodeURIComponent(options.q)
				const index = str.indexOf('{')
				let newStr = str.slice(index)
				// let obj = JSON.parse(newStr)
				uni.setStorageSync('params', newStr)
				// console.log(obj.type, obj.id)
				// console.log(typeof obj)
				// console.log(str)
			}
			if (uni.getStorageSync('login') && uni.getStorageSync('params')) {
				let params = JSON.parse(uni.getStorageSync('params'))
				this.$common.scanFun(params)
			}
			if (!uni.getStorageSync('login')) {
				this.showModal()
			}
		},
		onShow() {
			this.$common.setTitle(this.$config.communityName)
			if (this.personType == 1) {
				// console.log('触发')
				uni.setNavigationBarColor({
					frontColor: '#000000',
					backgroundColor: '#ffffff',
					animation: {
						duration: 400,
						timingFunc: 'easeIn'
					}
				})
				// this.getPersonInfo()

			}
			if (uni.getStorageSync('login')) {
				if (this.personType == 1) {
					this.getPersonInfo()
					return
				}
				this.init()
				this.getStatic()
			}
		},
		onReachBottom() {
			if (this.status1 !== 'noMore' && this.rectifyList && this.personType == 1) {
				this.status1 = 'loading'
				this.getMoreRectify()
			}
		},
		computed: {
			...mapState(['personType'])
		},
		methods: {
			...mapMutations(['setPersonType', 'setGridType']),
			goMyCheckPoint() {
				let phone = uni.getStorageSync('gridInfo').手机号
				this.$http.post('gridSystem/checkPoint/list', {负责人电话: phone}).then(res => {
					if(res.code === 10000) {
						res.data.list = res.data.list || []
						if(res.data.list.length === 1) {
							uni.navigateTo({
								url: '/fireGridCheck/myCheckPointDetail/myCheckPointDetail?id=' + res.data.list[0].id
							})
						}else {
							uni.navigateTo({
								url: '/fireGridCheck/myCheckPoint/myCheckPoint?list=' + JSON.stringify(res.data.list)
							})
						}
					}
				})
			},
			goAccount() {
				uni.navigateTo({
					url: '/fireGridCheck/uploadAccount/uploadAccount'
				})
			},
			goCheckRank() {
				uni.navigateTo({
					url: '/fireGridCheck/checkRank/checkRank'
				})
			},
			goMoreRectify() {
				console.log('点击触发')
				uni.navigateTo({
					url: '/fireGridCheck/moreRectifyTask/moreRectifyTask'
				})
			},
			goCheckItem() {
				uni.navigateTo({
					url: '/fireGridCheck/inspectItem/inspectItem'
				})
			},
			goCheckPoint(type) {
				if (!uni.getStorageSync('login')) {
					this.showModal()
				} else {
					uni.navigateTo({
						url: '/fireGridCheck/allCheckPoint/allCheckPoint?type=' + type
					})
				}
				
			},
			goResident(type) {
				if (!uni.getStorageSync('login')) {
					this.showModal()
				} else {
					uni.navigateTo({
						url: '/fireGridCheck/allResidentList/allResidentList?type=' + type
					})
				}
			},
			goInstallList() {
				if (!uni.getStorageSync('login')) {
					this.showModal()
				} else {
					uni.navigateTo({
						url: '/fireGridCheck/allInstallList/allInstallList'
					})
				}
			},
			async init() {
				// uni.showLoading()
				const userInfo = await this.getUserInfo()
				this.setGridType(userInfo.人员角色)
				uni.setStorageSync('gridInfo', userInfo)
			},
			showModal() {
				uni.showModal({
					title: '提示',
					content: '您还没有登录,请登录后使用',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/fireGridCheck/identityChoose/identityChoose'
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			// 获取责任人信息
			getPersonInfo() {
				this.$http.post('gridSystem/common/userinfo', {
					groupId: uni.getStorageSync('groupId')
				}).then(res => {
					console.log(res)
					if (res.code === 10000) {
						uni.setStorageSync('gridInfo', res.data.用户信息)
						// this.params2.所属网格id = res.data.用户信息.网格id
						this.params2.负责人电话 = res.data.用户信息.手机号
						this.params2.检查点ids = res.data.用户信息.关联的房屋类型检查点id
						this.getPersonRectify()
					}
				})
			},
			getPersonRectify() {
				this.params2.pagination.current = 1
				this.status1 = 'more'
				this.$http.post('gridSystem/check/solveList', this.params2).then(res => {
					if (res.code === 10000) {
						console.log(res)
						if (res.data.list !== null) {
							res.data.list.forEach(item => {
								item.整改开始日期 = dayjs(item.整改开始日期).format('YYYY-MM-DD')
							})
						}
						this.rectifyList = res.data.list
					}
				})
			},
			getMoreRectify() {
				this.params2.pagination.current++
				this.$http.post('gridSystem/check/solveList', this.params2).then(res => {
					if (res.code === 10000) {
						if (res.data.list !== null) {
							res.data.list.forEach(item => {
								item.整改开始日期 = dayjs(item.整改开始日期).format('YYYY-MM-DD')
							})
							this.rectifyList = this.rectifyList.concat(res.data.list)
						}
						// this.recordList = this.recordList && this.recordList.concat(res.data.list)
						if (this.rectifyList.length === res.data.total) {
							this.status1 = 'noMore'
						}
					}
				})
			},
			getUserInfo() {
				return new Promise((resolve, reject) => {
					this.$http.post('gridSystem/gridAdmin/info', {
						id: 0
					}).then(res => {
						if (res.code === 10000) {
							resolve(res.data)
						}
					})
				})
			},
			navItemClick(url) {
				if (!uni.getStorageSync('login')) {
					this.showModal()
				} else {
					uni.navigateTo({
						url
					})
				}
			},
			getStatic() {
				this.$http.post('gridSystem/summary/totalCount').then(res => {
					if(res.code === 10000) {
						console.log(res)
						this.rentHouseNum = res.data.出租屋总数,
						this.residentNum = res.data.居民总数,
						this.floatResidentNum = res.data.流动居民总数,
						this.permanentNum = res.data.常住居民总数
						this.gridNum = res.data.网格总数
						this.gridLeadNum = res.data.网格长总数
						this.gridPersonNum = res.data.网格员总数
						this.checkPointNum = res.data.检查点总数
						this.roomNum = res.data.房屋总数
						this.shopNum = res.data.商铺总数
						this.companyNum = res.data.公司总数
						this.restaurantNum = res.data.餐厅总数
						this.villageNum = res.data.小区总数
						this.publicNum = res.data.其他总数
						this.clinicNum = res.data.诊所总数
						this.facilityNum = res.data.设备总数
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	// @import '../../style/index.scss';
</style>
